package CCRMServer.GUI;
/**
 * This class implements the CCRM server GUI
 */
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.SwingConstants;

import CCRMServer.*;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;

public class MainServerWindow {

	private JFrame frame;
	private JTextField txtDbName;
	private JTextField txtDatabase;
	private JTextField txtUserName;
	private JTextField txtServerPort;
	private JTextField txtDBPassword;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainServerWindow window = new MainServerWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainServerWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 686, 464);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblConnectToDatabase = new JLabel("Connect to Database");
		lblConnectToDatabase.setFont(new Font("David", Font.BOLD, 14));
		lblConnectToDatabase.setBounds(20, 52, 129, 26);
		frame.getContentPane().add(lblConnectToDatabase);
		
		JLabel lblServerName = new JLabel("CCRM Server");
		lblServerName.setHorizontalAlignment(SwingConstants.CENTER);
		lblServerName.setForeground(Color.RED);
		lblServerName.setFont(new Font("David", Font.BOLD, 26));
		lblServerName.setBounds(0, 15, 670, 26);
		frame.getContentPane().add(lblServerName);
		
		txtDbName = new JTextField();
		txtDbName.setText("localhost");
		txtDbName.setHorizontalAlignment(SwingConstants.LEFT);
		txtDbName.setBounds(181, 97, 86, 20);
		frame.getContentPane().add(txtDbName);
		txtDbName.setColumns(10);
		
		txtDatabase = new JTextField();
		txtDatabase.setText("ccrm");
		txtDatabase.setHorizontalAlignment(SwingConstants.LEFT);
		txtDatabase.setColumns(10);
		txtDatabase.setBounds(181, 139, 86, 20);
		frame.getContentPane().add(txtDatabase);
		
		txtUserName = new JTextField();
		txtUserName.setText("root");
		txtUserName.setHorizontalAlignment(SwingConstants.LEFT);
		txtUserName.setColumns(10);
		txtUserName.setBounds(181, 182, 86, 20);
		frame.getContentPane().add(txtUserName);
		
		JLabel lblDbServerName = new JLabel("DB server name");
		lblDbServerName.setBounds(20, 100, 151, 14);
		frame.getContentPane().add(lblDbServerName);
		
		JLabel lblDatabaseName = new JLabel("Database name");
		lblDatabaseName.setBounds(20, 142, 151, 14);
		frame.getContentPane().add(lblDatabaseName);
		
		JLabel lblDbUserName = new JLabel("DB user name");
		lblDbUserName.setBounds(20, 185, 151, 14);
		frame.getContentPane().add(lblDbUserName);
		
		JLabel lblDbPassword = new JLabel("DB password");
		lblDbPassword.setBounds(20, 226, 151, 14);
		frame.getContentPane().add(lblDbPassword);
		
		JButton btnConnectToDB = new JButton("Set DB connection");
		btnConnectToDB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					String DBServer = txtDbName.getText();
					String DBName = txtDatabase.getText();
					String DBUser = txtUserName.getText();
					String DBPassword = txtDBPassword.getText();
					if (DBServer.isEmpty() || DBName.isEmpty() || DBUser.isEmpty() || DBPassword.isEmpty())
						JOptionPane.showMessageDialog(null, "Please fill all fields in order to set DB connection.", "DB Connection Error", JOptionPane.ERROR_MESSAGE);
					else {
						DBConnect.setMySQLConnection(DBServer, DBName, DBUser, DBPassword);
						JOptionPane.showMessageDialog(null, "Connection has been set", "DB connection", JOptionPane.INFORMATION_MESSAGE);
					}
				} catch (NullPointerException ex) {
					JOptionPane.showMessageDialog(null, "Please fill all fields in order to set DB connection.", "DB Connection Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnConnectToDB.setBounds(20, 254, 247, 23);
		frame.getContentPane().add(btnConnectToDB);
		
		JLabel lblStartServer = new JLabel("Start Server");
		lblStartServer.setFont(new Font("David", Font.BOLD, 14));
		lblStartServer.setBounds(399, 58, 129, 26);
		frame.getContentPane().add(lblStartServer);
		
		txtServerPort = new JTextField();
		txtServerPort.setHorizontalAlignment(SwingConstants.LEFT);
		txtServerPort.setText("11170");
		txtServerPort.setBounds(460, 97, 86, 20);
		frame.getContentPane().add(txtServerPort);
		txtServerPort.setColumns(10);
		
		JLabel lblServerPort = new JLabel("Server port");
		lblServerPort.setBounds(364, 100, 86, 14);
		frame.getContentPane().add(lblServerPort);
		
		JButton btnStartListening = new JButton("Start listening");
		btnStartListening.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (ServerStart.ccrm == null || !ServerStart.ccrm.isListening()) {
					ServerStart.initCCRMServer(txtServerPort.getText());
					if (ServerStart.ccrm.isListening())
						JOptionPane.showMessageDialog(null, "CCRM Server is now listening.", "Server Information", JOptionPane.INFORMATION_MESSAGE);
					else
						JOptionPane.showMessageDialog(null, "CCRM Server is not listening.", "Server Error", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "CCRM Server is already running.", "Server Information", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		btnStartListening.setBounds(373, 128, 173, 23);
		frame.getContentPane().add(btnStartListening);
		
		txtDBPassword = new JTextField();
		txtDBPassword.setHorizontalAlignment(SwingConstants.LEFT);
		txtDBPassword.setColumns(10);
		txtDBPassword.setBounds(181, 223, 86, 20);
		frame.getContentPane().add(txtDBPassword);
		
		JButton btnStopServer = new JButton("Stop Server");
		btnStopServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (ServerStart.ccrm != null && ServerStart.ccrm.isListening()) {
					ServerStart.ccrm.stopListening();
					try {
						ServerStart.ccrm.close();
					} catch (IOException ex) {
						JOptionPane.showMessageDialog(null, ex.getMessage(), "Server Error", JOptionPane.ERROR_MESSAGE);
					}
				}
				JOptionPane.showMessageDialog(null, "CCRM Server is not listening.", "Server Information", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnStopServer.setBounds(373, 254, 173, 23);
		frame.getContentPane().add(btnStopServer);
		frame.getContentPane().setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{txtDbName, txtDatabase, txtUserName, txtDBPassword, btnConnectToDB, txtServerPort, btnStartListening, btnStopServer}));
	}
}
